package com.hm.day45e.controller;

import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hm.day45e.biz.ProductsBiz;
import com.hm.day45e.biz.Type1Biz;
import com.hm.day45e.biz.Type2Biz;
import com.hm.day45e.model.AjaxMsgModel;
import com.hm.day45e.model.Products;
import com.hm.day45e.model.Resources;
import com.hm.day45e.model.Type1;
import com.hm.day45e.model.Type2;
import com.hm.day45e.model.Users;
import com.hm.day45e.util.BusinessException;
import com.hm.day45e.util.Constants;



@Controller
@RequestMapping("/products")
public class ProductsController {
	@Resource
	private ProductsBiz productsBiz;
	
	@Resource
	private Type1Biz type1Biz;
	
	@Resource
	private Type2Biz type2Biz;
	
	
	@RequestMapping("/list")
	public String list(int pageNum, Map<String, Object> request){
		try {
			long count = productsBiz.findCount();
			List<Products> list = productsBiz.findAll(pageNum);
			request.put("totalCount", count);
			request.put("list", list);
			request.put("pageNum", pageNum);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/products/list";
	}
	
	@ResponseBody
	@RequestMapping("/delete/{proId}")
	public AjaxMsgModel delete(@PathVariable int proId){
		try {
			Products pro = productsBiz.findById(proId);
			if(pro == null){
				return AjaxMsgModel.createFailModel(false);
			}
			productsBiz.delete(pro);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxMsgModel.createFailModel(false);
		}
		return AjaxMsgModel.createSuccessModel(false);
	}
	
	@RequestMapping("/preAdd")
	public String preAdd(Map<String, Object> request){
		try {
			request.put("pro", new Products());
			List<Type1> list = type1Biz.findAll();
			request.put("list", list);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/products/add";
	}
	
	@RequestMapping("/preUpdate/{proId}")
	public String preUpdate(@PathVariable int proId, Map<String, Object> request){
		try {
			Products pro = productsBiz.findById(proId);
			request.put("pro", pro);
			List<Type1> list = type1Biz.findAll();
			request.put("type1List", list);
			List<Type2> list1 = type2Biz.findByType1(pro.getType2().getType1().getT1Id());
			request.put("type2List", list1);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/products/update";
	}
	
	@RequestMapping("/findByType1")
	public void findByType1(int t1Id, PrintWriter out){
		try {
			List<Type2> list = type2Biz.findByType1(t1Id);
			StringBuffer sb = new StringBuffer("");
			for (Type2 type2 : list) {
				sb.append("<option value=\""+type2.getT2Id()+"\">"+type2.getName()+"</option>");
			}
			out.print(sb.toString());
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public AjaxMsgModel add(Products pro, MultipartHttpServletRequest request, HttpSession session){
		try {
			Users user = (Users) session.getAttribute("currentUser");
			MultipartFile file = request.getFile("file");
			String fileName = UUID.randomUUID().toString();
			File newFile = new File(Constants.FILE_PATH + fileName);
			file.transferTo(newFile);
			Resources res = new Resources();
			res.setResId(fileName);
			res.setResName(file.getOriginalFilename());
			res.setResPath(fileName);
			res.setResType(file.getContentType());
			res.setCreator(user.getTruename());
			res.setUpdator(user.getTruename());
			pro.setRes(res);
			pro.setCreator(user.getTruename());
			pro.setUpdator(user.getTruename());
			productsBiz.save(pro);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxMsgModel.createFailModel(false);
		}
		return AjaxMsgModel.createSuccessModel(true);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public AjaxMsgModel update(Products pro, MultipartHttpServletRequest request, HttpSession session){
		try {
			Users user = (Users) session.getAttribute("currentUser");
			MultipartFile file = request.getFile("file");
			if(file.getSize() != 0){
				String fileName = UUID.randomUUID().toString();
				File newFile = new File(Constants.FILE_PATH + fileName);
				file.transferTo(newFile);
				Resources res = new Resources();
				res.setResId(fileName);
				res.setResName(file.getOriginalFilename());
				res.setResPath(fileName);
				res.setResType(file.getContentType());
				res.setCreator(user.getTruename());
				res.setUpdator(user.getTruename());
				pro.setRes(res);
			}
			pro.setUpdator(user.getTruename());
			productsBiz.update(pro);
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxMsgModel.createFailModel(false);
		}
		return AjaxMsgModel.createSuccessModel(true);
	}
}
